﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WellDone
{
    public partial class EditEmployeeNameDialog : ChildWindow
    {
        public EditEmployeeNameDialog()
        {
            InitializeComponent();
            // Fill-in name
            tbFirstName.Text = Globals.selectedEmployee.col_FirstName;
            tbMiddleInitial.Text = Globals.selectedEmployee.col_MiddleInitial;
            tbLastName.Text = Globals.selectedEmployee.col_LastName;
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Ensure that a first and last name have been entered
            if (tbFirstName.Text.Trim().Length > 0 && tbLastName.Text.Trim().Length > 0)
            {
                //Update Selected Employee Name
                Globals.db.EmployeeRecords[Globals.employees.cmbEmployeeName.SelectedIndex].col_FirstName = tbFirstName.Text.Trim();
                Globals.db.EmployeeRecords[Globals.employees.cmbEmployeeName.SelectedIndex].bFilterFirstName = true;
                Globals.db.EmployeeRecords[Globals.employees.cmbEmployeeName.SelectedIndex].col_LastName = tbLastName.Text.Trim();
                Globals.db.EmployeeRecords[Globals.employees.cmbEmployeeName.SelectedIndex].bFilterLastName = true;
                Globals.db.EmployeeRecords[Globals.employees.cmbEmployeeName.SelectedIndex].col_MiddleInitial = tbMiddleInitial.Text.Trim();
                Globals.db.EmployeeRecords[Globals.employees.cmbEmployeeName.SelectedIndex].bFilterMiddleInitial = true;
                await Globals.db.UpdateEmployeeObject(Globals.db.EmployeeRecords[Globals.employees.cmbEmployeeName.SelectedIndex]);
                
                // Update the selected employee
                Globals.selectedEmployee.col_FirstName = tbFirstName.Text;
                Globals.selectedEmployee.col_MiddleInitial = tbMiddleInitial.Text;
                Globals.selectedEmployee.col_LastName = tbLastName.Text;
                Globals.selectedEmployee.bFilterFirstName = true;
                Globals.selectedEmployee.bFilterMiddleInitial = true;
                Globals.selectedEmployee.bFilterLastName = true;
                Globals.employees.cmbEmployeeName.Items[Globals.employees.cmbEmployeeName.SelectedIndex] = tbLastName.Text + ", " + tbFirstName.Text + " " + tbMiddleInitial.Text;

                this.DialogResult = true;
            }               
            else
                MessageBox.Show("Please provide a first and last name.");
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void tbLastName_KeyDown(object sender, KeyEventArgs e)
        {
            // Call the OK button handlers if "Enter" is pressed
            if (e.Key == Key.Enter)
                btnOK_Click(sender, e);
        }

    }
}

